package com.liam.unit.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

public class UnitClient {

    private static final String APP_ID = "22422197";
    private static final String API_KEY = "qNunApXuK7IycXcztjDBuwbp";
    private static final String SECRET_KEY = "IlTsSmYywWZ7M1ci3WikaiUKix0XvERS";

    private static final String BOT_ID = "1043897";


    /**
     * getToken获得，一个月有效，调试写死
     */
    private static final String token = "24.95a9710bdc07196589bec7f16cb13b91.2592000.1601446687.282335-22422197";

    /**
     * 一个月有效
     */
    public static void getToken(){

        String url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="+API_KEY+"&client_secret="+SECRET_KEY;

        try {
            JSONObject result = HttpUtil.httpPost(url,null);
            System.out.println(result.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static JSONObject talk(String content,String bootSession) throws IOException {
        String url = "https://unit-api.baidu.com/rpc/2.0/unit/bot/chat?access_token=" + token;
        url = "https://aip.baidubce.com/rpc/2.0/unit/bot/chat?access_token=" + token;

        JSONObject params = new JSONObject();
        params.put("version","2.0");
        params.put("bot_id",BOT_ID);
        params.put("log_id",System.currentTimeMillis());

        JSONObject request = new JSONObject();
        request.put("query",content);

        JSONObject query_info = new JSONObject();
        query_info.put("type","TEXT");
        query_info.put("source","KEYBOARD");

        request.put("query_info",query_info);
        request.put("bernard_level","1");
        request.put("user_id","xxx");

        JSONObject client_session = new JSONObject();
        client_session.put("client_results","");

        JSONArray candidate_options = new JSONArray();
        JSONObject candidate_option1 = new JSONObject();
        JSONObject attributes = new JSONObject();
        attributes.put("name","百度第一个大厦");
        attributes.put("seq","第一个");
        attributes.put("distance","2km");
        JSONObject slot_updates = new JSONObject();
        slot_updates.put("user_location","百度大厦");
        candidate_option1.put("attributes",attributes);
        candidate_option1.put("slot_updates",slot_updates);
        candidate_option1.put("remember",true);


        JSONObject candidate_option2 = new JSONObject();
        JSONObject attributes2 = new JSONObject();
        attributes2.put("name","百度科技园");
        attributes2.put("seq","第二个");
        attributes2.put("distance","2km");
        JSONObject slot_updates2 = new JSONObject();
        slot_updates2.put("user_location","百度科技园");
        candidate_option2.put("attributes",attributes2);
        candidate_option2.put("slot_updates",slot_updates2);
        candidate_option2.put("remember",true);

        candidate_options.add(candidate_option1);
        candidate_options.add(candidate_option2);
        client_session.put("candidate_options",candidate_options);


        request.put("client_session",client_session.toJSONString());
        params.put("request",request);

        params.put("bot_session",bootSession);

        System.err.println(params.toJSONString());
        return HttpUtil.httpPost(url,params);

    }

    public static void main(String[] args) {
//        getToken();
        try {
            String str = "打电话给小明";
            str = "你好嘟嘟";
            str = "打电话";
            str = "小明";

            str = "导航去西溪";
//            str = "1";

            String bootSession = "{\"bot_id\":\"1043897\",\"bot_views\":{\"bernard_res\":[{\"action_list\":[{\"action_id\":\"\",\"confidence\":0.0,\"custom_reply\":\"\",\"refine_detail\":{\"clarify_reason\":\"\",\"interact\":\"\",\"option_list\":[]},\"say\":\"\",\"type\":\"understood\"}],\"msg\":\"ok\",\"pre_nlu_outs\":[{\"otags_basic\":[{\"blength\":4,\"boffset\":6,\"ctype\":\"ENTITY_WORD\",\"eid\":\"\",\"entity_confidence\":0.0,\"etype\":\"sys_loc\",\"etypes\":[\"sys_loc\",\"sys_loc_generic\",\"user_location\"],\"father_fully_covered\":false,\"father_index\":-1,\"features\":[],\"formal\":\"\",\"formals\":[\"\",\"\",\"\"],\"fuzzy_formals\":[],\"importance\":-1.0,\"name\":\"西溪\",\"pat_extra\":\"\",\"pat_str\":\"prime_nerl\",\"pat_strs\":[],\"son_indexes\":[],\"tf_type\":1,\"tlength\":1,\"toffset\":2,\"type_confidence\":7.0}],\"otags_wpcomp\":[],\"polarity\":{\"label\":\"1\",\"pval\":0.9970},\"tokens_basic\":[{\"buffer\":\"导航\",\"length\":4,\"norm_degree\":0.9673960804939270,\"offset\":0,\"type\":36,\"weight\":0.4223187863826752},{\"buffer\":\"去\",\"length\":2,\"norm_degree\":0.0,\"offset\":4,\"type\":34,\"weight\":0.1553623825311661},{\"buffer\":\"西溪\",\"length\":4,\"norm_degree\":0.9337191581726074,\"offset\":6,\"type\":21,\"weight\":0.4223187863826752}],\"tokens_wpcomp\":[]}],\"qu_res\":{\"candidates\":[{\"confidence\":100.0,\"domain_confidence\":0.0,\"extra_info\":{\"group_id\":\"1\",\"real_threshold\":\"1\",\"threshold\":\"0.7\"},\"from_who\":\"pow-slu-lev1\",\"intent\":\"NAVIGATION\",\"intent_confidence\":100.0,\"intent_need_clarify\":false,\"match_info\":\"{\\\"group_id\\\":\\\"1\\\",\\\"id\\\":\\\"5710749\\\",\\\"informal_word\\\":\\\"去\\\",\\\"match_keywords\\\":\\\"  \\\",\\\"match_pattern\\\":\\\"导航\\\\t[D:user_location]\\\",\\\"ori_pattern\\\":\\\"导航\\\\t[D:user_location]\\\",\\\"ori_slots\\\":{\\\"confidence\\\":100.0,\\\"domain_confidence\\\":0.0,\\\"extra_info\\\":{},\\\"from_who\\\":\\\"smart_qu\\\",\\\"intent\\\":\\\"NAVIGATION\\\",\\\"intent_confidence\\\":100.0,\\\"intent_need_clarify\\\":false,\\\"match_info\\\":\\\"导航 \\\\t[D:user_location] \\\",\\\"slots\\\":[{\\\"begin\\\":6,\\\"confidence\\\":100.0,\\\"father_idx\\\":-1,\\\"fuzzy_matches\\\":[],\\\"length\\\":4,\\\"name\\\":\\\"user_location\\\",\\\"need_clarify\\\":false,\\\"normalized_word\\\":\\\"\\\",\\\"original_word\\\":\\\"西溪\\\",\\\"word_type\\\":\\\"\\\"}]},\\\"real_threshold\\\":1.0,\\\"threshold\\\":0.6999999880790710}\",\"slots\":[{\"begin\":6,\"confidence\":100.0,\"father_idx\":-1,\"fuzzy_matches\":[],\"length\":4,\"name\":\"user_location\",\"need_clarify\":false,\"normalized_word\":\"西溪\",\"original_word\":\"西溪\",\"word_type\":\"\"}]}],\"lexical_analysis\":[{\"basic_word\":[\"导航\"],\"etypes\":[],\"term\":\"导航\",\"type\":\"36\",\"weight\":0.4220},{\"basic_word\":[\"去\"],\"etypes\":[],\"term\":\"去\",\"type\":\"34\",\"weight\":0.1550},{\"basic_word\":[\"西溪\"],\"etypes\":[\"sys_loc\",\"sys_loc_generic\",\"user_location\"],\"term\":\"西溪\",\"type\":\"sys_loc\",\"weight\":0.4220}],\"qu_res_chosen\":\"{\\\"confidence\\\":100.0,\\\"domain_confidence\\\":0.0,\\\"extra_info\\\":{\\\"group_id\\\":\\\"1\\\",\\\"real_threshold\\\":\\\"1\\\",\\\"threshold\\\":\\\"0.7\\\"},\\\"from_who\\\":\\\"pow-slu-lev1\\\",\\\"intent\\\":\\\"NAVIGATION\\\",\\\"intent_confidence\\\":100.0,\\\"intent_need_clarify\\\":false,\\\"match_info\\\":\\\"{\\\\\\\"group_id\\\\\\\":\\\\\\\"1\\\\\\\",\\\\\\\"id\\\\\\\":\\\\\\\"5710749\\\\\\\",\\\\\\\"informal_word\\\\\\\":\\\\\\\"去\\\\\\\",\\\\\\\"match_keywords\\\\\\\":\\\\\\\"  \\\\\\\",\\\\\\\"match_pattern\\\\\\\":\\\\\\\"导航\\\\\\\\t[D:user_location]\\\\\\\",\\\\\\\"ori_pattern\\\\\\\":\\\\\\\"导航\\\\\\\\t[D:user_location]\\\\\\\",\\\\\\\"ori_slots\\\\\\\":{\\\\\\\"confidence\\\\\\\":100.0,\\\\\\\"domain_confidence\\\\\\\":0.0,\\\\\\\"extra_info\\\\\\\":{},\\\\\\\"from_who\\\\\\\":\\\\\\\"smart_qu\\\\\\\",\\\\\\\"intent\\\\\\\":\\\\\\\"NAVIGATION\\\\\\\",\\\\\\\"intent_confidence\\\\\\\":100.0,\\\\\\\"intent_need_clarify\\\\\\\":false,\\\\\\\"match_info\\\\\\\":\\\\\\\"导航 \\\\\\\\t[D:user_location] \\\\\\\",\\\\\\\"slots\\\\\\\":[{\\\\\\\"begin\\\\\\\":6,\\\\\\\"confidence\\\\\\\":100.0,\\\\\\\"father_idx\\\\\\\":-1,\\\\\\\"fuzzy_matches\\\\\\\":[],\\\\\\\"length\\\\\\\":4,\\\\\\\"name\\\\\\\":\\\\\\\"user_location\\\\\\\",\\\\\\\"need_clarify\\\\\\\":false,\\\\\\\"normalized_word\\\\\\\":\\\\\\\"\\\\\\\",\\\\\\\"original_word\\\\\\\":\\\\\\\"西溪\\\\\\\",\\\\\\\"word_type\\\\\\\":\\\\\\\"\\\\\\\"}]},\\\\\\\"real_threshold\\\\\\\":1.0,\\\\\\\"threshold\\\\\\\":0.6999999880790710}\\\",\\\"slots\\\":[{\\\"begin\\\":6,\\\"confidence\\\":100.0,\\\"father_idx\\\":-1,\\\"fuzzy_matches\\\":[],\\\"length\\\":4,\\\"name\\\":\\\"user_location\\\",\\\"need_clarify\\\":false,\\\"normalized_word\\\":\\\"西溪\\\",\\\"original_word\\\":\\\"西溪\\\",\\\"word_type\\\":\\\"\\\"}]}\\n\",\"raw_query\":\"导航去西溪\",\"sentiment_analysis\":{\"label\":\"1\",\"pval\":0.9970},\"status\":0,\"timestamp\":0},\"schema\":{\"domain_confidence\":0.0,\"intent\":\"NAVIGATION\",\"intent_confidence\":100.0,\"slots\":[{\"begin\":6,\"confidence\":100.0,\"fuzzy_matches\":[],\"length\":4,\"merge_method\":\"add\",\"name\":\"user_location\",\"normalized_word\":\"西溪\",\"original_word\":\"西溪\",\"session_offset\":0,\"sub_slots\":[],\"word_type\":\"\"}],\"slu_tags\":[]},\"status\":0}],\"bernard_status\":[{\"index\":0,\"step\":\"AFTER_DM_TRIGGER\"}],\"intervention\":{\"interv_qu_res\":\"\",\"interv_query\":\"\",\"qu_res_interved\":\"\",\"qu_res_original\":\"\",\"query_original\":\"\",\"type\":\"\",\"user_id\":\"\"}},\"dialog_state\":{\"contexts\":{},\"intents\":[{\"index\":0,\"name\":\"NAVIGATION\"}],\"slots\":{\"values\":[{\"begin\":6,\"confidence\":100.0,\"fuzzy_matches\":[],\"length\":4,\"merge_method\":\"add\",\"name\":\"user_location\",\"normalized_word\":\"西溪\",\"original_word\":\"西溪\",\"session_offset\":0,\"state\":2,\"sub_slots\":[],\"word_type\":\"\"}]},\"user_slots\":{\"user_location\":{\"attrs\":{\"default_state\":0,\"default_tag_name\":\"default\",\"example_values\":[],\"extensible\":false,\"name\":\"\",\"print\":[],\"type\":0,\"update_type\":\"\",\"weight\":0.0},\"state\":2,\"values\":{\"西溪\":{\"begin\":6,\"confidence\":100.0,\"length\":4,\"merge_method\":\"add\",\"normalized_name\":\"西溪\",\"original_name\":\"西溪\",\"session_offest\":0,\"state\":2,\"word_type\":\"\"}}}}},\"interactions\":[{\"interaction_id\":\"interaction-1600243568690-2276393964-8013-74\",\"request\":{\"bernard_level\":1,\"client_session\":\"{\\\"client_results\\\":\\\"\\\", \\\"candidate_options\\\":[]}\",\"hyper_params\":{\"slu_tags\":[]},\"query\":\"导航去西溪\",\"query_info\":{\"asr_candidates\":[],\"base_qu\":\"\",\"source\":\"KEYBOARD\",\"type\":\"TEXT\"},\"updates\":\"\",\"user_id\":\"xxx\"},\"response\":{\"action_list\":[{\"action_id\":\"navigation_satisfy\",\"confidence\":100.0,\"custom_reply\":\"\",\"refine_detail\":{\"clarify_reason\":\"\",\"interact\":\"\",\"option_list\":[]},\"say\":\"正在为你导航\",\"type\":\"satisfy\"}],\"msg\":\"ok\",\"qu_res\":{\"candidates\":[{\"confidence\":100.0,\"domain_confidence\":0.0,\"extra_info\":{\"group_id\":\"1\",\"real_threshold\":\"1\",\"threshold\":\"0.7\"},\"from_who\":\"pow-slu-lev1\",\"intent\":\"NAVIGATION\",\"intent_confidence\":100.0,\"intent_need_clarify\":false,\"match_info\":\"{\\\"group_id\\\":\\\"1\\\",\\\"id\\\":\\\"5710749\\\",\\\"informal_word\\\":\\\"去\\\",\\\"match_keywords\\\":\\\"  \\\",\\\"match_pattern\\\":\\\"导航\\\\t[D:user_location]\\\",\\\"ori_pattern\\\":\\\"导航\\\\t[D:user_location]\\\",\\\"ori_slots\\\":{\\\"confidence\\\":100.0,\\\"domain_confidence\\\":0.0,\\\"extra_info\\\":{},\\\"from_who\\\":\\\"smart_qu\\\",\\\"intent\\\":\\\"NAVIGATION\\\",\\\"intent_confidence\\\":100.0,\\\"intent_need_clarify\\\":false,\\\"match_info\\\":\\\"导航 \\\\t[D:user_location] \\\",\\\"slots\\\":[{\\\"begin\\\":6,\\\"confidence\\\":100.0,\\\"father_idx\\\":-1,\\\"fuzzy_matches\\\":[],\\\"length\\\":4,\\\"name\\\":\\\"user_location\\\",\\\"need_clarify\\\":false,\\\"normalized_word\\\":\\\"\\\",\\\"original_word\\\":\\\"西溪\\\",\\\"word_type\\\":\\\"\\\"}]},\\\"real_threshold\\\":1.0,\\\"threshold\\\":0.6999999880790710}\",\"slots\":[{\"begin\":3,\"confidence\":100.0,\"father_idx\":-1,\"fuzzy_matches\":[],\"length\":2,\"name\":\"user_location\",\"need_clarify\":false,\"normalized_word\":\"西溪\",\"original_word\":\"西溪\",\"word_type\":\"\"}]}],\"lexical_analysis\":[{\"basic_word\":[\"导航\"],\"etypes\":[],\"term\":\"导航\",\"type\":\"36\",\"weight\":0.4220},{\"basic_word\":[\"去\"],\"etypes\":[],\"term\":\"去\",\"type\":\"34\",\"weight\":0.1550},{\"basic_word\":[\"西溪\"],\"etypes\":[\"sys_loc\",\"sys_loc_generic\",\"user_location\"],\"term\":\"西溪\",\"type\":\"sys_loc\",\"weight\":0.4220}],\"qu_res_chosen\":\"{\\\"confidence\\\":100.0,\\\"domain_confidence\\\":0.0,\\\"extra_info\\\":{\\\"group_id\\\":\\\"1\\\",\\\"real_threshold\\\":\\\"1\\\",\\\"threshold\\\":\\\"0.7\\\"},\\\"from_who\\\":\\\"pow-slu-lev1\\\",\\\"intent\\\":\\\"NAVIGATION\\\",\\\"intent_confidence\\\":100.0,\\\"intent_need_clarify\\\":false,\\\"match_info\\\":\\\"{\\\\\\\"group_id\\\\\\\":\\\\\\\"1\\\\\\\",\\\\\\\"id\\\\\\\":\\\\\\\"5710749\\\\\\\",\\\\\\\"informal_word\\\\\\\":\\\\\\\"去\\\\\\\",\\\\\\\"match_keywords\\\\\\\":\\\\\\\"  \\\\\\\",\\\\\\\"match_pattern\\\\\\\":\\\\\\\"导航\\\\\\\\t[D:user_location]\\\\\\\",\\\\\\\"ori_pattern\\\\\\\":\\\\\\\"导航\\\\\\\\t[D:user_location]\\\\\\\",\\\\\\\"ori_slots\\\\\\\":{\\\\\\\"confidence\\\\\\\":100.0,\\\\\\\"domain_confidence\\\\\\\":0.0,\\\\\\\"extra_info\\\\\\\":{},\\\\\\\"from_who\\\\\\\":\\\\\\\"smart_qu\\\\\\\",\\\\\\\"intent\\\\\\\":\\\\\\\"NAVIGATION\\\\\\\",\\\\\\\"intent_confidence\\\\\\\":100.0,\\\\\\\"intent_need_clarify\\\\\\\":false,\\\\\\\"match_info\\\\\\\":\\\\\\\"导航 \\\\\\\\t[D:user_location] \\\\\\\",\\\\\\\"slots\\\\\\\":[{\\\\\\\"begin\\\\\\\":6,\\\\\\\"confidence\\\\\\\":100.0,\\\\\\\"father_idx\\\\\\\":-1,\\\\\\\"fuzzy_matches\\\\\\\":[],\\\\\\\"length\\\\\\\":4,\\\\\\\"name\\\\\\\":\\\\\\\"user_location\\\\\\\",\\\\\\\"need_clarify\\\\\\\":false,\\\\\\\"normalized_word\\\\\\\":\\\\\\\"\\\\\\\",\\\\\\\"original_word\\\\\\\":\\\\\\\"西溪\\\\\\\",\\\\\\\"word_type\\\\\\\":\\\\\\\"\\\\\\\"}]},\\\\\\\"real_threshold\\\\\\\":1.0,\\\\\\\"threshold\\\\\\\":0.6999999880790710}\\\",\\\"slots\\\":[{\\\"begin\\\":6,\\\"confidence\\\":100.0,\\\"father_idx\\\":-1,\\\"fuzzy_matches\\\":[],\\\"length\\\":4,\\\"name\\\":\\\"user_location\\\",\\\"need_clarify\\\":false,\\\"normalized_word\\\":\\\"西溪\\\",\\\"original_word\\\":\\\"西溪\\\",\\\"word_type\\\":\\\"\\\"}]}\\n\",\"raw_query\":\"导航去西溪\",\"sentiment_analysis\":{\"label\":\"1\",\"pval\":0.9970},\"status\":0,\"timestamp\":0},\"schema\":{\"domain_confidence\":0.0,\"intent\":\"NAVIGATION\",\"intent_confidence\":100.0,\"slots\":[{\"begin\":3,\"confidence\":100.0,\"fuzzy_matches\":[],\"length\":2,\"merge_method\":\"add\",\"name\":\"user_location\",\"normalized_word\":\"西溪\",\"original_word\":\"西溪\",\"session_offset\":0,\"sub_slots\":[],\"word_type\":\"\"}],\"slu_tags\":[]},\"status\":0},\"timestamp\":\"2020-09-16 16:06:08.690\"}],\"session_id\":\"session-1600243568606-2276393964-8013-16\"}";
            bootSession="";
            JSONObject result = talk(str,bootSession);

            System.out.println(result.toJSONString());
            result = result.getJSONObject("result");
            System.out.println("识别到意图："+result.getJSONObject("response").getJSONObject("schema").getString("intent"));
            System.out.println("session:" + result.getString("bot_session"));
            System.out.println("词槽："+result.getJSONObject("response").getJSONObject("schema").getJSONArray("slots").getJSONObject(0).getString("original_word"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
